package com.harveyguo.nihonngo.controller;

import com.harveyguo.nihonngo.model.dto.BoxingCornerDto;
import com.harveyguo.nihonngo.service.BoxingCornerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 拳击角Controller
 */
@RestController
@RequestMapping("/api/corners")
public class BoxingCornerController {

    @Autowired
    private BoxingCornerService boxingCornerService;

    /**
     * 创建拳击角
     */
    @PostMapping
    public ResponseEntity<BoxingCornerDto> createCorner(@RequestBody BoxingCornerDto cornerDto) {
        return ResponseEntity.ok(boxingCornerService.createCorner(cornerDto));
    }

    /**
     * 根据ID查找拳击角
     */
    @GetMapping("/{id}")
    public ResponseEntity<BoxingCornerDto> getCorner(@PathVariable String id) {
        return ResponseEntity.ok(boxingCornerService.findById(id));
    }

    /**
     * 查找所有拳击角
     */
    @GetMapping
    public ResponseEntity<List<BoxingCornerDto>> getAllCorners() {
        return ResponseEntity.ok(boxingCornerService.findAll());
    }

    /**
     * 根据拳馆ID查找拳击角
     */
    @GetMapping("/gym/{gymId}")
    public ResponseEntity<List<BoxingCornerDto>> getCornersByGym(@PathVariable String gymId) {
        return ResponseEntity.ok(boxingCornerService.findByGymId(gymId));
    }

    /**
     * 根据创建者ID查找拳击角
     */
    @GetMapping("/creator/{creatorId}")
    public ResponseEntity<List<BoxingCornerDto>> getCornersByCreator(@PathVariable String creatorId) {
        return ResponseEntity.ok(boxingCornerService.findByCreatorId(creatorId));
    }

    /**
     * 根据活跃状态查找拳击角
     */
    @GetMapping("/active/{active}")
    public ResponseEntity<List<BoxingCornerDto>> getCornersByActive(@PathVariable Boolean active) {
        return ResponseEntity.ok(boxingCornerService.findByActive(active));
    }

    /**
     * 根据技能水平查找拳击角
     */
    @GetMapping("/skill/{skillLevel}")
    public ResponseEntity<List<BoxingCornerDto>> getCornersBySkillLevel(@PathVariable String skillLevel) {
        return ResponseEntity.ok(boxingCornerService.findBySkillLevel(skillLevel));
    }

    /**
     * 查找附近的活跃拳击角
     */
    @GetMapping("/nearby")
    public ResponseEntity<List<BoxingCornerDto>> getNearbyActiveCorners(
            @RequestParam Double latitude,
            @RequestParam Double longitude,
            @RequestParam(defaultValue = "10.0") Double radius) {
        return ResponseEntity.ok(boxingCornerService.findNearbyActiveCorners(latitude, longitude, radius));
    }

    /**
     * 更新拳击角信息
     */
    @PutMapping("/{id}")
    public ResponseEntity<BoxingCornerDto> updateCorner(@PathVariable String id,
            @RequestBody BoxingCornerDto cornerDto) {
        return ResponseEntity.ok(boxingCornerService.updateCorner(id, cornerDto));
    }

    /**
     * 删除拳击角
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteCorner(@PathVariable String id) {
        boxingCornerService.deleteCorner(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 加入拳击角
     */
    @PostMapping("/{cornerId}/join")
    public ResponseEntity<BoxingCornerDto> joinCorner(@PathVariable String cornerId, @RequestParam String userId) {
        return ResponseEntity.ok(boxingCornerService.joinCorner(cornerId, userId));
    }

    /**
     * 退出拳击角
     */
    @PostMapping("/{cornerId}/leave")
    public ResponseEntity<BoxingCornerDto> leaveCorner(@PathVariable String cornerId, @RequestParam String userId) {
        return ResponseEntity.ok(boxingCornerService.leaveCorner(cornerId, userId));
    }
}